package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.daos.UmpireDao;
import model.dtos.Umpire;
import views.windows.RegisterUmpirePanel;

public class RegisterUmpireController extends RegisterUmpirePanel
{
	private static final long serialVersionUID = 1L;
	UmpireDao umpireDao;
	
	public RegisterUmpireController()
	{
		super();
		umpireDao = new UmpireDao();
		
		addActions();
	}
	
	public void cleanFields()
	{
		getCedulaTextField().setText("");
		getNombreTextField().setText("");
		getApellidoTextField().setText("");
		getFechaChooser().setDate(null);
	}
	
	public void addActions()
	{
		getRegistrarButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				
				if (getCedulaTextField().getText().trim().length() == 0 || getNombreTextField().getText().trim().length() == 0 ||
						getApellidoTextField().getText().trim().length() == 0 || getFechaChooser().getDate() == null)
				{
					showMessage("Datos Incompletos");
				}
				else
				{
					Umpire umpire = new Umpire();
					
					umpire.setIdentification(getCedulaTextField().getText());
					umpire.setName(getNombreTextField().getText());
					umpire.setLastName(getApellidoTextField().getText());
					umpire.setBirthday(getFechaChooser().getDate());
					
					umpireDao.save(umpire);
					
					showMessage("Umpire registrado con exito");
					
					cleanFields();
				}
			}
		});
		
		getCancelarButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				cleanFields();
			}
		});
	}
}
